<?php 

namespace App\Repositories;

class GroupRepository extends Repository 
{
    public function model() {
        return 'App\Models\Group';
    }

   	// 得到会员组下拉
   	public function memberGroupSelect()
   	{
   		return $this->getUserGroupSelect(0);
   	}

   	// 管理员组下拉
   	public function adminGroupSelect()
   	{
   		return $this->getUserGroupSelect(1);
   	}

   	// 获取用户组
   	public function getUserGroup($isAdmin=0)
   	{
   		return $this->model->where('is_admin', $isAdmin)->get();
   	}

   	// 获取用户组下拉
   	protected function getUserGroupSelect($isAdmin=0)
   	{
   		return $this->model->where('is_admin', $isAdmin)->lists('name', 'id');
   	}

    // 编译后台权限列表
    public function updatePermission()
    {
        $permissions = config('permissions');

        $compilerPermissions = [];

        foreach ($permissions as $key => $value) {
            foreach ($value['auth'] as $label => $controllers) {
                list($controller, $action) = explode('@', $controllers['controller']);

                if (strpos($action, '-') !== false) {
                    $action = explode('-', $action);
                }

                $action = (array)$action;

                foreach ($action as $fun) {
                   $compilerPermissions[$controller.'@'.$fun] = $label;
                }
            }
        }

        $content = "<?php \n return " . var_export($compilerPermissions, true) . ';';

        return file_put_contents(config_path('compile_permissions.php'), $content);
    }
}